import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse, InternalAxiosRequestConfig } from 'axios'
import { ElLoading } from 'element-plus'
import { LoadingInstance } from 'element-plus/lib/components/loading/src/loading.js'

interface AxiosUtilsInterceptors {
    requestInterceptor?: (config: InternalAxiosRequestConfig) => InternalAxiosRequestConfig
    requestInterceptorCatch?: (error: any) => any
    responseInterceptor?: (res: any) => any
    responseInterceptorCatch?: (error: any) => any
}

interface AxiosUtilsConfig extends AxiosRequestConfig {
    interceptors?: AxiosUtilsInterceptors
    showLoading?: boolean
}

const DEFAULT_LOADING = true

class AxiosUtils {
    instance: AxiosInstance
    interceptors?: AxiosUtilsInterceptors
    showLoading: boolean
    loading?: LoadingInstance

    constructor(config: AxiosUtilsConfig) {
        //创建axios实例
        this.instance = axios.create(config)
        //保存基本信息
        this.interceptors = config.interceptors
        this.showLoading = config.showLoading ?? DEFAULT_LOADING
        //拦截器的使用
        this.instance.interceptors.request.use((config) => {

            if (this.showLoading) {
                this.loading = ElLoading.service({
                    lock: true,
                    text: '加载中...',
                    background: 'rgba(0,0,0,0.3)'
                })
            }

            //const token = useUserStore().getToken
            const token = ''
            if (token) {
                config.headers.Authorization = `Bearer ${token}`
            }
            console.log("所有实例的请求拦截器");
            return config
        },
            (err) => {
                return err
            })
        this.instance.interceptors.request.use(
            this.interceptors?.requestInterceptor,
            this.interceptors?.requestInterceptorCatch
        )
        this.instance.interceptors.response.use(
            this.interceptors?.responseInterceptor,
            this.interceptors?.responseInterceptorCatch
        )
        this.instance.interceptors.response.use((res) => {
            this.loading?.close()
            // console.log("所有实例的返回拦截器");
            // const data = res.data;
            // if (data.returnCode === 200) {
            //     return res.data;
            // }
            // return "获取数据失败."
            return res.data;
        }, (err) => {
            this.loading?.close()
            if (err.response.status === 400) {
                console.log("404错误")
            }
            return err
        })
    }

    request<T>(config: AxiosUtilsConfig): Promise<T> {
        return new Promise((resolve, reject) => {
            //1、单个讲求对数据的处理
            if (config.interceptors?.requestInterceptor) {
                //config = config.interceptors.requestInterceptor(config)
            }
            //2、是否需求Loading
            if (config.showLoading === false) {
                this.showLoading = config.showLoading
            }
            this.instance.request<any, T>(config).then((res) => {
                //单个返回对数据的处理
                if (config.interceptors?.responseInterceptor) {
                    res = config.interceptors.responseInterceptor(res)
                }
                //恢复ShowLoading到默认值
                this.showLoading = DEFAULT_LOADING
                //将结果resolve返回
                resolve(res)
            }).catch((err) => {
                reject(err)
                this.showLoading = DEFAULT_LOADING
                return err;
            })
        })
    }

    get<T>(config: AxiosUtilsConfig): Promise<T> {
        return this.request<T>({ ...config, method: 'GET' })
    }
    post<T>(config: AxiosUtilsConfig): Promise<T> {
        return this.request<T>({ ...config, method: 'POST' })
    }
    delete<T>(config: AxiosUtilsConfig): Promise<T> {
        return this.request<T>({ ...config, method: 'DELETE' })
    }
    patch<T>(config: AxiosUtilsConfig): Promise<T> {
        return this.request<T>({ ...config, method: 'PATCH' })
    }

}







export default AxiosUtils